/*
 * MainMenu_Controller.java
 * Final Project Example
 * Created on Oct 8, 2014, 10:58:26 PM
 * by Fairul Filza with Team FA_PemrogrammanLanjut
 */

package Controllers;

// <editor-fold defaultstate="collapsed" desc="Import Code">
import Interfaces.ControllersProcess_Interface;
import Models.Dvds_Model;
import Views.MainMenu_MainView;
import java.awt.Cursor;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
// </editor-fold>

/** @author PemrogrammanLanjut_LABIV_TI_05~09 */
public class MainMenu_Controller implements ControllersProcess_Interface {
    private MainMenu_MainView MenuView;
    private Calendar CurrentDate;
    private SimpleDateFormat DateForm;
    private int flag;


    // <editor-fold defaultstate="collapsed" desc="Constructor Code">
    public MainMenu_Controller(MainMenu_MainView MenuView) {
        this.MenuView = MenuView;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Getter and Setter Code">
    public MainMenu_MainView getMenuView() {
        return MenuView;
    }

    public void setMenuView(MainMenu_MainView MenuView) {
        this.MenuView = MenuView;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Method Code">
    private void btnMenu_mouseClick(int code) {
        switch(code) {
            case FILE_EXIT:
                System.exit(0);
                break;
            case TOOLS_UNUSED:
                break;
            case HELP_ABOUT:
                break;
        }
    }

    private void btnMasterCd_mouseClick() {
        if(MenuView.getPanelTransaction().isVisible()){
            MenuView.getPanelTransaction().getBtnCancel().doClick();
        }
        if(!MenuView.getPanelTransaction().getBtnCancel().isVisible()) {
            MenuView.getPanelTransaction().setVisible(false);
            MenuView.getPanelCdSales().setVisible(false);
            MenuView.getPanelCategory().setVisible(false);
            MenuView.getPanelMasterCd().setVisible(true);
            MenuView.getLblStatus().setIcon(new ImageIcon(getClass().getResource("/Images/katalogcd.png")));
        }
    }
    
    private void btnTransaction_mouseClick() {
        if(!MenuView.getPanelTransaction().isVisible()) {
            MenuView.getPanelMasterCd().setVisible(false);
            MenuView.getPanelCdSales().setVisible(false);
            MenuView.getPanelCategory().setVisible(false);
            MenuView.getPanelTransaction().setVisible(true);
            MenuView.getLblStatus().setIcon(new ImageIcon(getClass().getResource("/Images/Transaction.png")));
        }
    }
    
    private void btnCdSales_mouseClick() {
        if(MenuView.getPanelTransaction().isVisible()){
            MenuView.getPanelTransaction().getBtnCancel().doClick();
        }
        if(!MenuView.getPanelTransaction().getBtnCancel().isVisible()) {
            MenuView.getPanelMasterCd().setVisible(false);
            MenuView.getPanelTransaction().setVisible(false);
            MenuView.getPanelCategory().setVisible(false);
            MenuView.getPanelCdSales().setVisible(true);
            MenuView.getLblStatus().setIcon(new ImageIcon(getClass().getResource("/Images/sales3.png")));
        }
    }
    private void btnCategory_mouseClick() {
        if(MenuView.getPanelTransaction().isVisible()){
            MenuView.getPanelTransaction().getBtnCancel().doClick();
        }
        if(!MenuView.getPanelTransaction().getBtnCancel().isVisible()) {
            MenuView.getPanelMasterCd().setVisible(false);
            MenuView.getPanelTransaction().setVisible(false);
            MenuView.getPanelCdSales().setVisible(false);
            MenuView.getPanelCategory().setVisible(true);
            MenuView.getLblStatus().setIcon(new ImageIcon(getClass().getResource("/Images/category3.png")));
        }
    }

    private void btnSearch_mouseClick(){
        MenuView.getPanelMasterCd().setSearch(true);
        btnMasterCd_mouseClick();
        Dvds_Model Model;
        DefaultTableModel Record;
        String[] Header;
        int[] wCol;

        Header = new String[] {"TITLE", "CODE", "CATEGORY", "STOCK", "PRICE", "DETAIL"};
        Model = new Dvds_Model(0, MenuView.getTxtSearch().getText());
        Model.showSearchDataSet();
        Record = new DefaultTableModel(Model.getDataRecord(), Header){
            @Override
            public boolean isCellEditable(int row, int column) {
            //Only the third column
            return false;
        }
        };
        wCol = new int[]{300, 50,100 ,70 ,70, 150};
        MenuView.getPanelMasterCd().getTblData().setModel(Record);
        initColumnRecord(MenuView.getPanelMasterCd().getTblData(), wCol);
//        ListView.getTblData().addRowSelectionInterval(0, 0);
//        ListView.getLblCode().setText("");
        
        MenuView.getPanelMasterCd().getBtnNew().setIcon(new ImageIcon(getClass().getResource("/Images/new.png")));
        MenuView.getPanelMasterCd().getBtnNew().setEnabled(true);
        MenuView.getPanelMasterCd().getBtnEdit().setIcon(new ImageIcon(getClass().getResource("/Images/edit3.png")));
        MenuView.getPanelMasterCd().getBtnEdit().setEnabled(false);
        MenuView.getPanelMasterCd().getBtnDelete().setIcon(new ImageIcon(getClass().getResource("/Images/delete3.png")));
        MenuView.getPanelMasterCd().getBtnDelete().setEnabled(false);
    }
    
    private void btnExit_mouseClick() {
        System.exit(0);
    }

    private void initColumnRecord(JTable table, int[] wCol) {
        TableColumn col = null;
        int i = 0;
        for (i = table.getColumnCount() - 1; i > 0; i--) {
            col = table.getColumnModel().getColumn(i);
            col.setPreferredWidth(wCol[i]);
        }
    }
    
    private void frmMain_windowClosing() {
        if(JOptionPane.showConfirmDialog(null, "Are You Sure?", "Warning",
                JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            System.exit(0);
        }
    }

    private void curDate_runtimeDate() {
        String Patern = null;

        CurrentDate = Calendar.getInstance();
        DateForm = new SimpleDateFormat("dd MMMM yyyy HH:mm");
        Patern = DateForm.format(CurrentDate.getTime());
        MenuView.getLblDate().setText(Patern.substring(Patern.length() - 10, Patern.length() - 5));
        MenuView.getLblMonth().setText(Patern.substring(0, (Patern.length() - 10)));
        MenuView.getLblTime().setText(Patern.substring(Patern.length() - 5));
        
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Anonymous Code">
    public ActionListener MainButton_Listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent Evt) {
           if(Evt.getSource().equals(MenuView.getBtnMasterCd())) {
                btnMasterCd_mouseClick(); 
          }
           else if(Evt.getSource().equals(MenuView.getBtnTransaction())) {
                btnTransaction_mouseClick(); 
          }
           else if(Evt.getSource().equals(MenuView.getBtnDvdSales())) {
                btnCdSales_mouseClick(); 
          }
           else if(Evt.getSource().equals(MenuView.getBtnCategory())) {
                btnCategory_mouseClick(); 
          }
           else if(Evt.getSource().equals(MenuView.getBtnExit())) {
                frmMain_windowClosing();
           }
           else if(Evt.getSource().equals(MenuView.getBtnSearch())){
               btnSearch_mouseClick();
           }
        }
    };

    public WindowAdapter MainWindow_Listener = new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent Evt) {
            frmMain_windowClosing();
        }
    };

    public MouseAdapter MainButtonImg_Listener = new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent Evt) {
            if(Evt.getSource().equals(MenuView.getBtnMasterCd())) {
                MenuView.getBtnMasterCd().setIcon(new ImageIcon(getClass().getResource("/Images/Mastercd_btn2.png")));
            }
            else if(Evt.getSource().equals(MenuView.getBtnTransaction())) {
                MenuView.getBtnTransaction().setIcon(new ImageIcon(getClass().getResource("/Images/Transaction_btn2.png")));
            }
            else if(Evt.getSource().equals(MenuView.getBtnDvdSales())) {
                MenuView.getBtnDvdSales().setIcon(new ImageIcon(getClass().getResource("/Images/dvdsales_btn2.png")));
            }
            else if(Evt.getSource().equals(MenuView.getBtnCategory())) {
                MenuView.getBtnCategory().setIcon(new ImageIcon(getClass().getResource("/Images/category2.png")));
            }
            else if(Evt.getSource().equals(MenuView.getBtnExit())) {
                MenuView.getBtnExit().setIcon(new ImageIcon(getClass().getResource("/Images/exit2.png")));
            }
        }

        @Override
        public void mouseReleased(MouseEvent Evt) {
            
            if(Evt.getSource().equals(MenuView.getBtnMasterCd())) {
                MenuView.getBtnMasterCd().setIcon(new ImageIcon(getClass().getResource("/Images/Mastercd_btn.png")));
            }
            else if(Evt.getSource().equals(MenuView.getBtnTransaction())) {
                MenuView.getBtnTransaction().setIcon(new ImageIcon(getClass().getResource("/Images/Transaction_btn.png")));
            }
            else if(Evt.getSource().equals(MenuView.getBtnDvdSales())) {
                MenuView.getBtnDvdSales().setIcon(new ImageIcon(getClass().getResource("/Images/dvdsales_btn.png")));
            }
            else if(Evt.getSource().equals(MenuView.getBtnCategory())) {
                MenuView.getBtnCategory().setIcon(new ImageIcon(getClass().getResource("/Images/category.png")));
            }
            else if(Evt.getSource().equals(MenuView.getBtnExit())) {
                MenuView.getBtnExit().setIcon(new ImageIcon(getClass().getResource("/Images/exit.png")));
            }
        }
        @Override
        public void mouseEntered(MouseEvent Evt){
            if(Evt.getSource().equals(MenuView.getBtnMasterCd())) {
                MenuView.getBtnMasterCd().setCursor(new Cursor(Cursor.HAND_CURSOR));
            }
            else if(Evt.getSource().equals(MenuView.getBtnTransaction())) {
                MenuView.getBtnTransaction().setCursor(new Cursor(Cursor.HAND_CURSOR));
            }
            else if(Evt.getSource().equals(MenuView.getBtnDvdSales())) {
                MenuView.getBtnDvdSales().setCursor(new Cursor(Cursor.HAND_CURSOR));
            }
            
            else if(Evt.getSource().equals(MenuView.getBtnExit())) {
                MenuView.getBtnExit().setCursor(new Cursor(Cursor.HAND_CURSOR));
            }
        }
        @Override
        public void mouseExited(MouseEvent Evt){
            if(Evt.getSource().equals(MenuView.getBtnMasterCd())) {
                MenuView.getBtnMasterCd().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }
            else if(Evt.getSource().equals(MenuView.getBtnTransaction())) {
                MenuView.getBtnTransaction().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }
            else if(Evt.getSource().equals(MenuView.getBtnDvdSales())) {
                MenuView.getBtnDvdSales().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }
            
            else if(Evt.getSource().equals(MenuView.getBtnExit())) {
                MenuView.getBtnExit().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }
        }
    };

    public KeyAdapter MenuKey_Listener = new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent Evt) {
            if(Evt.getSource().equals(MenuView.getTxtSearch())) {
                if(Evt.getKeyCode() == KeyEvent.VK_ENTER) {
                    btnSearch_mouseClick();
                }
            } 
        }

    };
    
    public ActionListener CurrentDate_ActListener = new ActionListener() {
       @Override
        public void actionPerformed(ActionEvent Evt) {
            curDate_runtimeDate();
        }
    };

    public ActionListener LabelAnimate_ActListener = new ActionListener() {
        int i = (-530 / 2); // Sumber Bedasarkan panjang label yang akan di gerakan

        @Override
        public void actionPerformed(ActionEvent e) {
            i++;  // Pergerakan text untuk Motion Maju

            if(i == 530) i = -530; // Jika sudah mencapai titik akhir text kembali ke awal

            // Untuk Text bekedip
            if(i % 20 == 0) {  // Untuk setiap hitungan per 10
              //  MenuView.getLblContext().setSize((MenuView.getLblContext().getWidth() == 0) ? 185 : 0, 32); // Kedipkan text
            }
            //MenuView.getLblTitle().setLocation(i + 10, 0);  // Pergerakan Text
        }
    };
    // </editor-fold>
}
// End of Code